package com.tjise.eduorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjise.commonutils.JwtUtils;
import com.tjise.commonutils.R;
import com.tjise.eduorder.entity.Order;
import com.tjise.eduorder.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author TJISE
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/eduorder/order")
@Api(description = "生成订单")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //1 生成订单的方法
    @ApiOperation("生成订单的方法")
    @PostMapping("createOrder/{courseId}")
    public R savaOrder(@PathVariable String courseId, HttpServletRequest request){
        //创建订单 返回订单号
        String orderNo = orderService.createOrders(courseId,JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderNo);
    }

    //根据订单id查询订单信息
    @ApiOperation(value = "根据订单id查询订单信息")
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }
    // 根据课程id还有用户id查询订单表中的订单的状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable ("courseId") String courseId,@PathVariable("memberId")String memberId){
        QueryWrapper<Order> wrapper =new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        wrapper.eq("course_id",courseId);
        wrapper.eq("status",1);
        // 查询订单表中时候有这个记录
        int result = orderService.count(wrapper);
        if (result > 0 ) {
            // 有数据就说明买过
            return true;
        }else {

        // 没有数据就是没有支付过
            return  false;
        }
    }
}

